import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:flutter_widgets_pc/widgets/chart/kq_chart_model.dart';
import '../../resources/kq_theme_colors.dart';
import 'kq_chart_line_model.dart';

class KqChartBarModel extends GetxController {
  KqChartModel chartModel;

  ///折线数据集合
  List<List<ChartBarValue>> chartBarArry;

  ///是否增加线状图,默认为false
  bool isLine;

  ///折线数据集合
  List<List<ChartLineValue>>? chartLineArry;

  /**以下均为内部计算无需传入 */
  ///实际高度
  double yHeight = 0;

  ///实际宽度
  double xWidth = 0;

  ///横坐标间距
  double crossSpacing = 0;

  ///横坐标数组
  List xList = [];

  ///横坐标内容
  List xValue = [];

  ///纵坐标数组
  List yList = [];

  ///纵坐标内容
  List yValue = [];

  KqChartBarModel({
    required this.chartBarArry,
    required this.chartModel,
    this.chartLineArry,
    this.isLine = false,
  });

  //初始化内容
  void _initData() {
    chartModel.initData();
    yHeight = chartModel.sizeHeight - chartModel.yTop - chartModel.yBottom;
    xWidth = chartModel.sizeWidth - chartModel.xLeft - chartModel.xRight;
  }

  ///设置内容
  void setValueList(List<List<ChartBarValue>> barlist,
      {List<List<ChartLineValue>>? lineList}) {
    chartModel.crossCount = chartModel.crossCount == 0
        ? barlist.first.length
        : chartModel.crossCount;
    _initData();
    chartBarArry = barlist;
    chartLineArry = lineList;
    if (!chartModel.isFixMaxY) {
      chartBarArry.map((List<ChartBarValue> e) {
        e.map((ChartBarValue value) {
          double y = double.parse(value.yValue);
          chartModel.maxY = chartModel.maxY > y ? chartModel.maxY : y;
        }).toList();
      }).toList();
      if (chartLineArry!.isNotEmpty && chartLineArry != null) {
        chartLineArry!.map((List<ChartLineValue> e) {
          e.map((ChartLineValue value) {
            double y = double.parse(value.yValue);
            chartModel.maxY = chartModel.maxY > y ? chartModel.maxY : y;
          }).toList();
        }).toList();
      }
    }
    setChartData();
    setXpointToBarValueList();
    setYpointToBarValueList();
    if (chartLineArry!.isNotEmpty && chartLineArry != null) {
      setXpointToLineValueList();
      setYpointToLineValueList();
    }
  }

  ///获取横纵坐标
  void setChartData() {
    getAbscissaList();
    getOrdinateList();
  }

  ///获取横坐标内容
  void getAbscissaList() {
    crossSpacing = xWidth / (chartModel.crossCount - 1);
    xList = [];
    for (int i = 0; i < chartModel.crossCount; i++) {
      double absPoint = crossSpacing * i + chartModel.xLeft;
      xList.add(absPoint);
    }
  }

  ///获取纵坐标内容
  void getOrdinateList() {
    double rowSpacing = yHeight / (chartModel.rowCount - 1);
    double ySpac = chartModel.maxY / (chartModel.rowCount - 1);
    yList = [];
    yValue = [];
    for (int i = 0; i < chartModel.rowCount; i++) {
      yValue.add((ySpac * (chartModel.rowCount - i - 1)).toString());
      yList.add(rowSpacing * i + chartModel.yTop);
    }
  }

  ///给柱状内容数组赋值横坐标值
  void setXpointToBarValueList() {
    xValue = [];
    List<List<ChartBarValue>> barXlist = [];
    for (int i = 0; i < chartModel.crossCount; i++) {
      List<ChartBarValue> xIndex = [];
      for (int j = 0; j < chartBarArry.length; j++) {
        ChartBarValue value = chartBarArry[j][i];
        xIndex.add(value);
      }
      barXlist.add(xIndex);
    }
    for (int n = 0; n < barXlist.length; n++) {
      double xPonit = xList[n];
      double sum = barXlist[n].fold(
          0, (double preValue, ChartBarValue data) => preValue + data.barWidth);
      double leftPonit = xPonit - sum / 2;
      xValue.add(barXlist[n].first.xValue);
      for (int m = 0; m < barXlist[n].length; m++) {
        ChartBarValue item = barXlist[n][m];
        item.xPoint = leftPonit + item.barWidth / 2;
        leftPonit = leftPonit + item.barWidth;
      }
    }
  }

  ///给线内容数组赋值横坐标值
  void setXpointToLineValueList() {
    xValue = [];
    for (int j = 0; j < chartLineArry!.length; j++) {
      List<ChartLineValue> valueList = chartLineArry![j];
      for (int i = 0; i < valueList.length; i++) {
        ChartLineValue value = valueList[i];
        value.xPoint = xList[i];
      }
    }
  }

  ///给柱状内容数组赋值纵坐标值
  void setYpointToBarValueList() {
    for (int j = 0; j < chartBarArry.length; j++) {
      List<ChartBarValue> valueList = chartBarArry[j];
      for (int i = 0; i < valueList.length; i++) {
        ChartBarValue value = valueList[i];
        double yHvalue = double.parse(value.yValue);
        value.yPoint =
            ((chartModel.maxY - yHvalue) / chartModel.maxY) * yHeight +
                chartModel.yTop;
      }
    }
  }

  ///给线内容数组赋值纵坐标值
  void setYpointToLineValueList() {
    for (int j = 0; j < chartLineArry!.length; j++) {
      List<ChartLineValue> valueList = chartLineArry![j];
      for (int i = 0; i < valueList.length; i++) {
        ChartLineValue value = valueList[i];
        double yHvalue = double.parse(value.yValue);
        value.yPoint =
            ((chartModel.maxY - yHvalue) / chartModel.maxY) * yHeight +
                chartModel.yTop;
      }
    }
  }
}

class ChartBarValue {
  String xValue;
  String yValue;

  ///文案样式
  TextStyle style;

  ///当点击或者滑动到该区域时是否显示文案，只针对isShowValue=fase且isShowClickValue或isShowSlideValue为True才会被使用
  bool isShow;

  ///柱状颜色
  Color barColor;

  ///柱状宽度，默认5
  double barWidth;

  ///以下为自动计算，无需传入
  double xPoint = 0;
  double yPoint = 0;

  ChartBarValue(
      {required this.xValue,
      required this.yValue,
      this.barWidth = 0,
      this.style = const TextStyle(fontSize: 12, color: KqThemeColors.text59),
      this.isShow = false,
      this.barColor = KqThemeColors.bgBlue});
}
